package com.fbt.transfer.app.driver.baidu;

import android.content.Context;
import android.os.*;
import android.view.*;
import android.widget.*;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import com.baidu.navisdk.adapter.BaiduNaviManagerFactory;
import com.baidu.navisdk.adapter.IBNRouteResultManager;
import com.baidu.navisdk.adapter.struct.BNRoutePlanInfos;
import com.baidu.navisdk.adapter.struct.BNRoutePlanItem;
import com.fbt.transfer.app.driver.R;
import com.fbt.transfer.app.driver.util.LogUtils;
import java.util.ArrayList;

/** 路线规划结果 */
public class RouteResultFragment extends Fragment {
  private static final String TAG = "RouteResultFragment";
  public static final int DELAY_MILLIS = 1000;

  /** 算路重试 */
  private FrameLayout mFlRetry;

  /** 所有路线方案 */
  private ArrayList<BNRoutePlanItem> mRoutePlanItems;

  private View mRootView;
  private RouteInfoListener routeInfoListener;
  private FragmentActivity fragmentActivity;

  // 定义路线信息监听接口
  public interface RouteInfoListener {
    void onRouteInfoCalculated(int timeMinutes, int distanceKm, int trafficLights);
  }

  @Override
  public void onAttach(@NonNull Context context) {
    super.onAttach(context);
    // 尝试获取接口实现
    if (context instanceof RouteInfoListener) {
      routeInfoListener = (RouteInfoListener) context;
    }
  }

  @Nullable
  @Override
  public View onCreateView(
      @NonNull LayoutInflater inflater,
      @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    fragmentActivity = getActivity();
    BaiduNaviManagerFactory.getRouteResultManager().onCreate(fragmentActivity);
    mRootView = inflater.inflate(R.layout.fragment_route_result, container, false);
    mFlRetry = mRootView.findViewById(R.id.fl_retry);
    BaiduNaviManagerFactory.getRouteResultSettingManager()
        .setRealRoadCondition(fragmentActivity, true);
    BaiduNaviManagerFactory.getRouteResultSettingManager().setRouteMargin(100, 100, 100, 500);
    initListener();
    BaiduNaviManagerFactory.getRouteResultManager().showTruckLimitIcon(true);
    return mRootView;
  }

  private void initListener() {
    BaiduNaviManagerFactory.getRouteResultManager()
        .setCalcRouteByViaListener(
            new IBNRouteResultManager.ICalcRouteByViaListener() {
              @Override
              public void onStart() {
                LogUtils.i(TAG, "开始");
              }

              @Override
              public void onSuccess() {
                mFlRetry.setVisibility(View.GONE);
              }

              @Override
              public void onFailed(int errorCode) {
                mFlRetry.setVisibility(View.VISIBLE);
              }
            });
  }

  @Override
  public void onResume() {
    super.onResume();
    BaiduNaviManagerFactory.getRouteResultManager().onResume();
    FrameLayout ybContainer = mRootView.findViewById(R.id.yb_container);
    BaiduNaviManagerFactory.getRouteResultManager().addYellowTipsToContainer(ybContainer);
  }

  @Override
  public void onPause() {
    super.onPause();
    BaiduNaviManagerFactory.getRouteResultManager().onPause();
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    BaiduNaviManagerFactory.getRouteResultManager().onDestroy();
  }

  private void initData() {
    BNRoutePlanInfos routePlaneInfos =
        BaiduNaviManagerFactory.getRoutePlanManager().getRoutePlanInfo();
    ArrayList<BNRoutePlanItem> mRoutePlanItems = routePlaneInfos.getRouteTabInfos();
    BNRoutePlanItem bnRoutePlanItem = mRoutePlanItems.get(0);
    // 获取路线信息
    int timeMinutes = (int) bnRoutePlanItem.getPassTime() / 60;
    int distanceKm = (int) bnRoutePlanItem.getLength() / 1000;
    int trafficLights = bnRoutePlanItem.getLights();
    // 通过接口回调将路线信息传递给Activity
    if (routeInfoListener != null) {
      routeInfoListener.onRouteInfoCalculated(timeMinutes, distanceKm, trafficLights);
    }
  }
}
